package pe.edu.upla.lamerced.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;

import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.service.HabitacionService;
import pe.edu.upla.lamerced.utiles.Utiles;

public class TipoHabitacionAction extends DispatchAction {
	
	private static final Logger logger = Logger.getLogger(TipoHabitacionAction.class);
	
	@Autowired
	HabitacionService habitacionService;


	public ActionForward lista(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
            logger.debug(" lista TIPO DE HABITACION");
           request.setAttribute("lstTipoHabitacion", habitacionService.listaTipoHabitacion());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("procesosHome");
		}
		return mapping.findForward("lista");
	}
	
	public ActionForward preNuevo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
            logger.debug(" PRE NUEVO TIPO DE HABITACION");
           
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("procesosHome");
		}
		return mapping.findForward("nueva");
	}
	
	public ActionForward preEdit(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
			String cod = request.getParameter("cod");
			Tipohabitacion hab = habitacionService.obtenerTipoHabitacion(Integer.parseInt(cod));
			DynaActionForm myform=(DynaActionForm)form;
			myform.set("idtipoHabitacion", hab.getIdtipoHabitacion().toString());
			myform.set("nombre", hab.getNombre());
			myform.set("detalle", hab.getDetalle());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
		}
		return mapping.findForward("nueva");
	}
	
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
            logger.debug("NUEVO TIPO DE HABITACION");
            DynaActionForm miform = (DynaActionForm)form;
            Tipohabitacion th = new Tipohabitacion();
            th.setNombre(miform.getString("nombre"));
            th.setDetalle(miform.getString("detalle"));
            if(!Utiles.nullToBlank(miform.getString("idtipoHabitacion")).equals("")){
            	th.setIdtipoHabitacion(Integer.parseInt(miform.getString("idtipoHabitacion")));
            }            	
            habitacionService.registrarTipoHabitacion(th);
            th = habitacionService.obtenerTipoHabitacion(th.getIdtipoHabitacion());
            request.setAttribute("mensaje", "tipo rgistrado");
            request.setAttribute("dth", th);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("nueva");
		}
		return mapping.findForward("confirmar");
	}
	
	public ActionForward cancelaNuevo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
            logger.debug(" cancelar NUEVO TIPO DE HABITACION");
            request.setAttribute("Mensaje", "Cancelado");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
		}
		return mapping.findForward("procesosHome");
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
			String cod = request.getParameter("cod");
			request.setAttribute("dth", habitacionService.obtenerTipoHabitacion(Integer.parseInt(cod)));
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
		}
		return mapping.findForward("confirmar");
	}
}
